package com.taxicalculator.ui;

import java.text.DecimalFormat;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.taxicalculator.dao.ILocationSQLite;
import com.taxicalculator.dao.ITaxiCalculatorDAO;
import com.taxicalculator.dao.LocationsSQLite;
import com.taxicalculator.dao.TaxiCalculatorDAO;
import com.taxicalculator.service.CalculatorService;
import com.taxicalculator.service.ICalculatorService;

public class CalcActivity extends Activity {
	
	private String toAddress;
	private String fromAddress;
	private int fromLocationType = 0;
	private int toLocationType = 1;
	private double distance;
	double people;
	String pple;
	ITaxiCalculatorDAO TaxiCalculator = new TaxiCalculatorDAO();
	ICalculatorService calc = new CalculatorService();
	ILocationSQLite locationSQLite = new LocationsSQLite(this);
	AddressToLatLong addressToLatLong = new AddressToLatLong();
	
	/**
	 * @author Thomas
	 * creates the Intent layout and gets to and from address text to display to user
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.calculatemain);
		//Sets hint for to address
		toAddress = locationSQLite.getLocation(toLocationType);
		EditText edt = (EditText) findViewById(R.id.edTxtToAddress);
		edt.setHint(toAddress);
		//sets the text view with string from GEO Corder
		fromAddress = locationSQLite.getLocation(fromLocationType);
		TextView tv = (TextView) findViewById(R.id.lblFromAddress);
		tv.setText(fromAddress);
		
	}
	/**
	 * @author Martin Manger
	 * calcClick uses the users given inputs, and finishCalculation to update the cost of the trip  
	 */
	public void calcClick(View v) {
		
		if (distance != 0){
			finishCalculation(1);
		}else{
		EditText edt;
		edt = (EditText) findViewById(R.id.edTxtToAddress);
		toAddress = edt.getText().toString();
		
		addressToLatLong.execute();
		}
	}
	/**
	 * @author Martin Manger
	 * divideClick takes the value calculated in calcClick and uses the values given by the user 
	 * converts the cost into a per-person cost
	 * does not effect the original cost
	 */
	public void divideClick(View V){
		
		people = 1;
		EditText p = (EditText) findViewById(R.id.divideCostLabel);
		pple = p.getText().toString();
		
		if (distance != 0){
			finishCalculation(2);
			EditText pt = (EditText) findViewById(R.id.divideCostLabel);
			pt.setText("#");
		}else{
			EditText edt;
			edt = (EditText) findViewById(R.id.edTxtToAddress);
			toAddress = edt.getText().toString();
			
			addressToLatLong.execute();
			
		}
	
	}
	/**
	 * @author Thomas Maco
	 * calls main Activity page when paused
	 */
	@Override
	protected void onPause() {
		
		super.onPause();
		 Intent MainActivity = new Intent(this, MainActivity.class);
		 startActivity(MainActivity);
	}
	/**
	 * @author Thomas Maco and Martin Manger
	 * runs the code to do all calculation
	 * requires an int to differentiate between which calculation is being done.
	 */
	public void finishCalculation(int i){
		EditText p = (EditText) findViewById(R.id.divideCostLabel);
		String pple = p.getText().toString();
		
		EditText m = (EditText) findViewById(R.id.mpgInput);
		String mpg = m.getText().toString();
		EditText x = (EditText) findViewById(R.id.gasInput);
		String cost = x.getText().toString();
		double output = 1;
		double mpgDouble = 1;
		double costDouble = 1;

		try {
			mpgDouble = Double.parseDouble(mpg);
			costDouble = Double.parseDouble(cost);
			output = calc.fuelCost(distance, mpgDouble, costDouble);
			if (people == 1){
				try{
					people = Double.parseDouble(pple);
					output = calc.costPer(output, people);
				} catch (Exception e){
					e.printStackTrace();
					Toast.makeText(CalcActivity.this, "Something went wrong.", Toast.LENGTH_LONG).show();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			Toast.makeText(CalcActivity.this, "Something went wrong.", Toast.LENGTH_LONG).show();
		}
		output = RoundToTwoDecimals(output);
		String label = "'$" + output + "'";
		TextView t;
		if (i == 1){
			t = (TextView) findViewById(R.id.calculateView);
		}else{
			t = (TextView) findViewById(R.id.costPerRiderOutput);
		}
		
		t.setText(label);
	}
	/**
	 * Formats double into the correct format
	 * @author Thomas Maco
	 * @param val value passing in to be converted
	 * @return formatted value
	 */
	double RoundToTwoDecimals(double val) {
        DecimalFormat df2 = new DecimalFormat("###.##");
    return Double.valueOf(df2.format(val));
}
	
/**
 * 
 * @author Thomas Maco
 *starts new thread for network operations
 */
	class AddressToLatLong extends AsyncTask<TaxiCalculatorDAO, Integer, String> {
		/**
		 * calls last method 
		 */
		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			finishCalculation(1);
		}
		/**
		 * calls getDistance method on TaxiCalculatorDAO
		 */
		@Override
		protected String doInBackground(TaxiCalculatorDAO... arg0) {
			String returnResultfake = " ";
			try {
				distance = TaxiCalculator.getDistance(toAddress, CalcActivity.this);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return returnResultfake;
		}
	}
}
